import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:login/config/SystemConfig.dart';
import 'package:login/pages/register1.dart';

class RegisterPage extends StatefulWidget {
  RegisterPage({Key key}) : super(key: key);

  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  //定义一个测试的电话号码
  String _tel = ' ';
  void _sendPhone() {
    Navigator.push(context,
        new MaterialPageRoute(builder: (BuildContext context) {
      //new一个路由表，构造方法，实例化builder
      //将电话传参给这个页面对象
      return RegisterPage1(tel: _tel); //构造方法实例化
    }));
  }

  //
  void _checkRegister(_tel) async {
    //请求后台
    String api = SystemConfig.BASE_URL +
        '/appUser/checkRegister?mobile=' +
        _tel.toString();
    Response response = await Dio().get(api);
    Map result = json.decode(response.toString());
    //判断电话号码是否被注册过
    if (result != null && result['success']) {
      Navigator.pushNamed((context), '/register1');
      this._sendPhone();
    } else {
      Fluttertoast.showToast(
          msg: result['message'],
          backgroundColor: Colors.black87,
          toastLength: Toast.LENGTH_LONG,
          fontSize: 20.0,
          gravity: ToastGravity.BOTTOM);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            '注册',
            // style: TextStyle(color: Colors.black, fontSize: 20.0),
          ),
          // color: Colors.yellowAccent,
        ),
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: ListView(
            children: <Widget>[
              //输入框，请输入手机号码
              Container(
                //container只能放一个child
                margin: EdgeInsets.only(bottom: 20.0),
                child: TextField(
                  autofocus: true,
                  keyboardType: TextInputType.number,
                  cursorColor: Colors.orangeAccent,
                  //hintText副文本
                  decoration: InputDecoration(
                      prefixIcon: Icon(Icons.perm_identity),
                      hintText: '请输入手机号码',
                      hintStyle: TextStyle(color: Colors.blue)),
                  onChanged: (value) {
                    _tel = value;
                  },
                ),
              ),

              //下一步按钮
              Container(
                padding:EdgeInsets.only(left: 20,right: 20),
                child:RaisedButton(
                   child: Text(
                    '下一步',
                   style: TextStyle(color: Colors.white, fontSize: 14.0),
                  ),
                color: Colors.blue[300],

                //事件为null禁用
                onPressed: () {
                   
                  // Navigator.pushNamed(context, '/register1');
                  this._checkRegister(_tel);
                },
                shape: new RoundedRectangleBorder(
                    //设置为圆角
                    borderRadius: BorderRadius.circular(20),
                    //无边框
                    side: BorderSide.none),
              )
              ),
            ],
          ),
        ));
  }
}
